package org.lee.whois;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * WhoisClient
 * whois协议使用43端口，查询域名及ip信息
 *
 * @author dralee
 * @date 2021-12-28 16:57:55
 **/
public class WhoisClient {
    public static void main(String[] args) {
        String domain = "haoshuaji.com";
        String whoisServer = "whois.internic.net";//"whois.nic.me";
        int port = 43;

        try (Socket socket = new Socket(whoisServer, port)) {
            try (PrintWriter writer = new PrintWriter(socket.getOutputStream(), true)) {
                writer.println(domain);
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
